<template>
  <view @tap="handleItemClick"
    class="bg-white rounded-[28rpx] p-[32rpx] shadow-md relative flex flex-col hover:shadow-lg transition-all duration-300 transform hover:-translate-y-1">
    <!-- 头部信息 -->
    <view class="flex justify-between items-center mb-[18rpx]">
      <view class="text-[32rpx] font-bold text-[#222] flex-1 truncate">{{
        item.title
      }}</view>
    </view>

    <view
      class="my-[10rpx] p-[18rpx] bg-gradient-to-r from-[#e6f7ff] to-[#bae7ff] rounded-[12rpx] border-l-[6rpx] border-l-[#1890ff]"
    >
      <view class="flex justify-between text-[24rpx] mb-[6rpx] line-clamp-2">
        任务描述：{{ item.describe_str }}
      </view>
    </view>

    <!-- 核心数据 -->
    <view class="flex items-center mb-[18rpx]">
      <view class="flex items-center">
        <IconFont font-class-name="iconfont" class-prefix="icon" name="rijie" color="#d32f2f" size="20" class="mr-[8rpx]"/>

        <view class="text-[24rpx] font-bold text-[#999]">
          金额：¥{{ Number(item.total_amount).toFixed(2) }}
        </view>
      </view>
      <view class="text-[24rpx] text-[#666]">
      </view>
      <view class="text-[24rpx] text-[#666] ml-auto">截止日期: {{ item.deadline }}</view>
    </view>

    <!-- 标签 -->
    <view class="flex flex-wrap gap-x-[10rpx] gap-y-[10rpx] mb-[10rpx]">
      <view v-for="(tag, tIndex) in item.tags" :key="tIndex"
        class="text-[20rpx] px-[16rpx] py-[6rpx] bg-[#e3f2fd] rounded-[10rpx] text-[#1565c0] font-medium">
        {{ tag }}
      </view>
    </view>

    <!-- 底部操作区 -->
    <view class="flex justify-end items-center mt-[10rpx] pt-[10rpx] border-t border-[#f5f7fa] gap-[12rpx]">
      <nut-button size="small" type="info" @click.stop="handleViewDetails">
        查看详情
      </nut-button>

      <nut-button v-if="item.memberStatus == 5" size="small" disabled>
        已放弃任务
      </nut-button>
      <nut-button v-else size="small" type="warning" @click.stop="handleGiveUp">
        放弃任务
      </nut-button>


    </view>
  </view>
</template>

<script setup>
import Taro from "@tarojs/taro";
import { IconFont } from "@nutui/icons-vue-taro";
import { Button as nutButton } from "@nutui/nutui-taro";
import { post } from "@/utils/request";
import { ref } from "vue";
import EvaluateModal from "@/components/EvaluateModal.vue";

function getStatus(status) {
  if (status == 11) {
    return "已通过"
  }
  if (status == 10) {
    return "已拒绝"
  }
  return ""
}


/**
 * 接受申请
 * @param {Object} applicant - 申请用户信息
 */
const handleAccept = (applicant) => {
  // if (applicant.status === 'accepted' || applicant.status === 'rejected') {
  //   return;
  // }

  Taro.showModal({
    title: "确认接受",
    content: `确定要接受 ${applicant.name} 的申请吗？`,
    success: (res) => {
      if (res.confirm) {
        // 更新申请状态
        // applicant.status = 'accepted';
        acceptApplicant(applicant);

        Taro.showToast({
          title: "已提交",
          icon: "success",
          duration: 2000,
        });
        // 这里可以调用API提交接受申请
        console.log("接受申请：", applicant.id);
      }
    },
  });
};


// 接收父组件传入的单个任务项数据
const props = defineProps({
  item: {
    type: Object,
    required: true,
    default: () => ({}),
  }
});

// 向父组件传递事件
const emit = defineEmits(["click", "viewDetails", "submit", "giveUp", "reload"]);

// 处理整个项点击
const handleItemClick = () => {
  emit("click", props.item);
};

// 处理查看详情
const handleViewDetails = () => {
  emit("viewDetails", props.item);
};

// 处理评价
const handleSubmit = () => {
  emit("submit", props.item);
};

// 处理放弃任务
const handleGiveUp = () => {
  console.log("props.item", props.item)
  Taro.showModal({
    title: '确认放弃',
    content: '确定要放弃这个任务吗？',
    success: (res) => {
      if (res.confirm) {
        post("/task/registercancel", { id: props.item.tmId }).then(res => {
          if (res.code == 0) {
            Taro.showToast({
              title: '放弃成功',
              icon: 'success'
            })
            emit("reload");
          } else {
            Taro.showToast({
              title: res.msg,
              icon: 'error'
            })
          }

        })
        //emit("giveUp", props.item);
      }
    }
  })
};
</script>